package com.example.xinhuazidian.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.xinhuazidian.R
import com.example.xinhuazidian.bean.Xiehouyu

class XHYAdapter(private val list: MutableList<Xiehouyu>) :
    RecyclerView.Adapter<XHYAdapter.MyViewHolder>() {

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ll: LinearLayout = view.findViewById(R.id.ll)
        val tvRiddle: TextView = view.findViewById(R.id.tvRiddle)
        val tvAnswer: TextView = view.findViewById(R.id.tvAnswer)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_xiehouyu, parent, false)
        return MyViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val appInfo = this.list[position]
        with(holder) {
            tvRiddle.text = appInfo.riddle
            tvAnswer.text = "—— ${appInfo.answer}"

            if (appInfo.zhanKai == false) {
                tvAnswer.visibility = View.GONE
            } else {
                tvAnswer.visibility = View.VISIBLE
            }

            ll.setOnClickListener {
                if (appInfo.zhanKai == false) {
                    tvAnswer.visibility = View.VISIBLE
                    appInfo.zhanKai = true
                } else {
                    tvAnswer.visibility = View.GONE
                    appInfo.zhanKai = false
                }
            }
        }
    }

    override fun getItemCount() = list.size
}

